//
//  HMStatusPictureView.swift
//  新浪微博
//
//  Created by 饶鹏伟 on 16/4/13.
//  Copyright © 2016年 饶鹏伟. All rights reserved.
//

import UIKit
import SDWebImage

//cell的重用标示符
private let pictrueCellId = "pictrueCellId"
//图片之间的间距
private let pictureCellMargin: CGFloat = 3
//CollectionView的最大宽度
private let maxWidth = ScreenWidth-2*StatusCellMargin
//每个item的最大宽度
private let itemWidth = (maxWidth-2*pictureCellMargin)/3

class HMStatusPictureView: UICollectionView {

   //外界设置的图片模型数组
    var imageURLs: [HMStatusPicture]?{
    
        didSet{
            
            //根据模型数组的数量 计算配图视图的大小
            //print(imageURLs?.count)
            let pSize = caculatePictureViewSize()
            //更新视图的大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            
            ///更新测试数据
            testLabel.text = "\(imageURLs?.count ?? 0)"
            
            //刷新页面的数据
            self.reloadData()
            
        }
    }
    
    
  //根据配图的数量计算配图的大小
    
   //单张图片显示原比例 四张图片显示2*2 多张图片显示3*n 每行三个
    private func caculatePictureViewSize() -> CGSize{
        
        //图片数量
        let imgCount = imageURLs?.count ?? 0
        
        if imgCount==0{
            
            return CGSizeZero
        }
        
        //视图的自动布局
        let layOut = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //单张图片
        if imgCount==1{
            //先设置一个固定大小
            let imageSize=CGSize(width: 100, height: 150)
            
            //将layout的itemsize设置和配图一般大
            layOut.itemSize=imageSize
            
            return imageSize
        }
        
        //四张图片
        layOut.itemSize=CGSize(width: itemWidth, height: itemWidth)
        if imgCount==4{
            
            let w=itemWidth*2 + pictureCellMargin
            
            return CGSize(width: w, height: w)
        }
        
        //多张图片
        let row=CGFloat((imgCount-1)/3)+1//几行
        let h = row * itemWidth + (row - 1) * pictureCellMargin
        
        return CGSize(width: maxWidth, height: h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layOut = UICollectionViewFlowLayout()
        
        layOut.minimumInteritemSpacing=pictureCellMargin
        layOut.minimumLineSpacing=pictureCellMargin
        
        super.init(frame: frame, collectionViewLayout: layOut)
        
        //设置视图
        setupUI()
        
        //注册cell
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictrueCellId)
        
        //设置数据源代理
        self.dataSource = self
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

    }
    
    
    // 懒加载 添加一个label 显示图片数量 便于观察
    private lazy var testLabel: UILabel = UILabel(text: "", textColor: UIColor.redColor(), fontSize: 18)
    
}

//显示图片视图的扩展
extension HMStatusPictureView: UICollectionViewDataSource{
    
    //每组返回多少行cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageURLs?.count ?? 0
    }
    
    // 没个cell返回什么数据
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictrueCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.statusPicture = imageURLs![indexPath.item]
        
        return cell
    }
}

//自定义cell

class PictureCell: UICollectionViewCell {
    
    
    var statusPicture: HMStatusPicture? {
        
        didSet{
        //给图片赋值
        let url = NSURL(string: statusPicture?.thumbnail_pic ?? "")
            iconView.sd_setImageWithURL(url)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置ui界面
    private func setupUI(){
        
        //iconView和cell一样大
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
  //懒加载子视图 显示图片的视图
    private lazy var iconView: UIImageView = {
        
        let v=UIImageView()
        //设置填充模式
        v.contentMode = .ScaleAspectFill
        //手写代码需要手动设置裁剪
        v.clipsToBounds = true
        return v
    }()
}





